package okw.gui;

import java.util.ArrayList;
import java.util.Random;

import okw.database.DatabaseManager;
import okw.database.Token;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import core.modules.Printer;

public class TokenGenerator {
	Shell shell = null;
	Group dane = null;
	
	public TokenGenerator(Shell parent){
		shell  = new Shell(parent, SWT.TITLE|SWT.SYSTEM_MODAL| SWT.CLOSE | SWT.MAX);
		ile();
		MainWindowO.center(shell);
		shell.open();
	}
	
	private void ile(){
		dane = new Group(shell, SWT.NONE); 
		
		Label label = new Label(dane, SWT.CENTER);
		label.setText("Ile tokenow wygnerowac?");
		label.setLocation(20,20);
		label.pack();
		
		final Text txt = new Text(dane, SWT.BORDER);
		txt.setLocation(20,40);
		txt.setSize(20,100);
		txt.pack();
		
		Button button = new Button(dane, SWT.PUSH);
		button.setText("Generuj");
		button.setLocation(20,70);
		button.pack();
		
		button.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent arg0) {
				System.out.println(txt.getText());
				try{
					Integer ile = Integer.parseInt(txt.getText());
					if(ile<0)
						throw new Exception();
					dane.dispose();
					generuj(ile); 
				} catch (Exception e){
					MessageBox mb = new MessageBox(shell);
					mb.setMessage("Podaj poprawna liczbe");
					mb.open();
				}
					
				
			}
			
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});
	
		dane.pack();
		shell.pack();
	}
	
	private void generuj(int ile){
		dane = new Group(shell, SWT.NONE); 
		
		Random r = new Random();
		final ArrayList<String> lista = new ArrayList<String>();
		DatabaseManager db = DatabaseManager.getInstance();
		
		for(int i = 0; i<ile; i++){
			Token token = null;
			while(true){
				Integer l = r.nextInt(100000);
				token = new Token(l.toString());
				if(db.save(token) != null) {
					lista.add(token.getValue());
					break;
				}	
			}
		}
		
		Table tokens = new Table(dane, SWT.BORDER | SWT.SINGLE);
		tokens.setLocation(0,0);
		tokens.setSize(150,300);
		for(String s: lista){
			TableItem ti = new TableItem(tokens, SWT.NONE);
			ti.setText(s);
		}
		
		Button button = new Button(dane, SWT.PUSH);
		button.setText("Drukuj");
		button.setLocation(60,320);
		button.pack();
		
		button.addSelectionListener(new SelectionListener() {
			
			public void widgetSelected(SelectionEvent arg0) {
				String str[] = new String[lista.size()];
				lista.toArray(str);
				Printer printer = new Printer();
				printer.drukuj(str);
				
			}
			
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});

		dane.pack();
		shell.pack();
	}

}
